import { WarningFilled,SuccessFilled,CircleCloseFilled } from '@element-plus/icons-vue'

export function confirmMessageBox(icon:any,message: string, confirmButtonText: string = '确定', cancelButtonText: string = '取消', confirmFn?: Function, cancelFn?: Function) {
    // 根据 icon 确定对应的类名
    const iconClass = {
        'dialog-warn-icon': icon === WarningFilled,
        'dialog-success-icon': icon === SuccessFilled,
        'dialog-error-icon': icon === CircleCloseFilled
    }

    // 根据 icon 确定 MessageBox 的类型
    let boxType: 'warning' | 'success' | 'error' = 'warning';
    if (icon === SuccessFilled) {
        boxType = 'success';
    } else if (icon === CircleCloseFilled) {
        boxType = 'error';
    }

    ElMessageBox.confirm(
        h('div', { class: 'flex flex-col flex-center pt-20px' }, [
            h(icon, { class: iconClass }),
            h('p', message)
        ]),
        '提示',
        {
            confirmButtonText,
            cancelButtonText,
            type: boxType,
            center: true,
            customClass: 'customize-message-box',
            closeOnClickModal:false
        }
    ).then(() => {
        try {
            const result = confirmFn?.();
            if (result instanceof Promise) {
                return result.catch((err) => {
                    console.error('confirmFn 异常:', err);
                });
            }
        } catch (err) {
            console.error('confirmFn 同步异常:', err);
        }
    }).catch(() => {
        cancelFn?.();
    });
}